<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item
        :label="$t('performance_group_name')"
        prop="performanceGroupName"
      >
        <el-input
          v-model="queryParams.performanceGroupName"
          :placeholder="
            $t('please_enter', { name: $t('performance_group_name') })
          "
          :show-word-limit="true"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        :label="$t('performance_group_code')"
        prop="performanceGroupCode"
      >
        <el-input
          v-model="queryParams.performanceGroupCode"
          :placeholder="
            $t('please_enter', { name: $t('performance_group_code') })
          "
          :show-word-limit="true"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >{{ $t("search") }}</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{
          $t("reset")
        }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['performance:performanceGroup:add']"
          >{{ $t("add") }}</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="groupList"
      @selection-change="handleSelectionChange"
      border
      :max-height="tableHeight"
    >
      <el-table-column
        :label="$t('performance_group_name')"
        align="center"
        prop="performanceGroupName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('performance_group_code')"
        align="center"
        prop="performanceGroupCode"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('remarks')"
        align="center"
        prop="remark"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('action')"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['performance:performanceGroup:view']"
            >{{ $t("view") }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['performance:performanceGroup:edit']"
            >{{ $t("modify") }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['performance:performanceGroup:delete']"
            >{{ $t("delete") }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
      :page-sizes="[10,20,30,50,100, 200]"
    />

    <!-- 添加或修改绩效组对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
        :disabled="lookView"
      >
        <el-card class="box-card" style="margin-bottom: 20px">
          <div slot="header" class="clearfix">
            <span>{{ $t("basic_information") }}</span>
          </div>
          <el-form-item
            :label="$t('performance_group_name')"
            prop="performanceGroupName"
          >
            <el-input
              v-model="form.performanceGroupName"
              :placeholder="
                $t('please_enter', { name: $t('performance_group_name') })
              "
              :show-word-limit="true"
            />
          </el-form-item>
          <el-form-item
            :label="$t('performance_group_code')"
            prop="performanceGroupCode"
          >
            <el-input
              v-model="form.performanceGroupCode"
              :placeholder="
                $t('please_enter', { name: $t('performance_group_code') })
              "
              :show-word-limit="true"
            />
          </el-form-item>
          <el-form-item :label="$t('remarks')" prop="remark">
            <el-input
              v-model="form.remark"
              :placeholder="$t('please_enter', { name: $t('remarks') })"
              :rows="4"
              :show-word-limit="true"
              type="textarea"
            />
          </el-form-item>
        </el-card>

        <el-card class="box-card" style="margin-bottom: 20px">
          <div slot="header" class="clearfix">
            <span>{{ $t("performance_appraisal") }}</span>
          </div>
          <performanceModal
            :checkData="performanceModalList"
            :lookView="lookView"
            ref="performanceModal"
            v-if="open"
          ></performanceModal>
        </el-card>
        <el-card class="box-card" style="margin-bottom: 20px">
          <div slot="header" class="clearfix">
            <span>{{ $t("employer_configuration") }}</span>
          </div>
          <el-form-item :label="$t('select_employing_company')">
            <employerListSelect
              :employerlist="employerlist"
              @geteMployerList="geteMployerList"
            ></employerListSelect>
          </el-form-item>
        </el-card>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!lookView">
        <el-button type="primary" @click="submitForm">{{
          $t("confirm")
        }}</el-button>
        <el-button @click="cancel">{{ $t("cancel") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listGroup,
  getGroup,
  delGroup,
  addGroup,
  updateGroup,
} from "@/api/custom/performanceGroup.js";
import { getAllEmployer } from "@/api/custom/employerList";
import performanceModal from "./performanceModal.vue";
import employerListSelect from "@/views/components/custom/employerListSelect";
export default {
  name: "Group",
  components: {
    performanceModal,
    employerListSelect,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 绩效组表格数据
      groupList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        performanceGroupName: null,
        performanceGroupCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        performanceGroupName: [
          {
            required: true,
            message: this.$t("cannot_empty", {
              name: this.$t("performance_group_name"),
            }),
            trigger: "blur",
          },
        ],
        performanceGroupCode: [
          {
            required: true,
            message: this.$t("cannot_empty", {
              name: this.$t("performance_group_code"),
            }),
            trigger: "blur",
          },
        ],
      },
      lookView: false,
      employerList: [],
      performanceModalList: [],
      employerlist: [],
    };
  },
  created() {
    this.getList();
    this.getAllEmployer();
  },
  methods: {
    geteMployerList(list) {
      this.form.employerList = list.map((item) => {
        return {
          ...item,
          employerId: item.id,
        };
      });
    },
    getAllEmployer() {
      getAllEmployer().then((res) => {
        this.employerList = res.data;
      });
    },
    /** 查询绩效组列表 */
    getList() {
      this.loading = true;
      listGroup(this.queryParams).then((response) => {
        this.groupList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        performanceGroupName: null,
        performanceGroupCode: null,
        remark: null,
        meritsList: [],
      };
      this.employerlist = [];
      this.lookView = false;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = this.$t("add");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getGroup(id).then((response) => {
        this.form = response.data;
        if (this.form.employerList.length > 0) {
          let list = [];
          this.form.employerList.forEach((element) => {
            list.push(element.employer);
          });
          this.employerlist = list;
        }
        this.performanceModalList = response.data.meritsList.map(
          (item) => item.performance
        );
        this.open = true;
        this.title = this.$t("modify");
      });
    },
    handleView(row) {
      this.reset();
      const id = row.id || this.ids;
      getGroup(id).then((response) => {
        this.form = response.data;
        if (this.form.employerList.length > 0) {
          let list = [];
          this.form.employerList.forEach((element) => {
            list.push(element.employer);
          });
          this.employerlist = list;
        }
        this.performanceModalList = response.data.meritsList.map(
          (item) => item.performance
        );
        this.open = true;
        this.lookView = true;
        this.title = this.$t("view");
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let checkList = this.$refs.performanceModal.checkList;
          if (checkList.length) {
            this.form.meritsList = checkList.map((item) => {
              return {
                performanceId: item.id,
              };
            });
          }
          if (!this.form.employerList.length) {
            this.$message.warning(this.$t("select_employer"));
            return;
          }
          if (this.form.id != null) {
            updateGroup(this.form).then((response) => {
              this.$modal.msgSuccess(this.$t("operation_successful"));
              this.open = false;
              this.getList();
            });
          } else {
            addGroup(this.form).then((response) => {
              this.$modal.msgSuccess(this.$t("operation_successful"));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(this.$t("delete_data"))
        .then(function () {
          return delGroup(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t("operation_successful"));
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/group/export",
        {
          ...this.queryParams,
        },
        `performanceGroup_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
